home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / PRINTMSG.C < prev    next >
C/C++ Source or Header  |  1993-04-10  |  6KB  |  192 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    p r i n t m s g . c                                             */
  3. /*                                                                    */
  4. /*    Support routines for UUPC/extended                              */
  5. /*                                                                    */
  6. /*    Changes Copyright 1990, 1991 (c) Andrew H. Derbyshire           */
  7. /*                                                                    */
  8. /*    History:                                                        */
  9. /*       21Nov1991 Break out of lib.c                          ahd    */
  10. /*--------------------------------------------------------------------*/
  11.  
  12. /*
  13.  *    $Id: PRINTMSG.C 1.3 1993/04/10 21:26:04 ahd Exp $
  14.  *
  15.  *    $Log: PRINTMSG.C $
  16.  *     Revision 1.3  1993/04/10  21:26:04  ahd
  17.  *     Use unique buffer for printmsg() time stamp
  18.  *
  19.  * Revision 1.2  1992/11/20  12:39:37  ahd
  20.  * Move heapcheck to check heap *EVERY* call
  21.  *
  22.  */
  23.  
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <time.h>
  29.  
  30. #ifdef __CORE__
  31. #define __HEAPCHECK__
  32. #endif
  33.  
  34. #ifdef __HEAPCHECK__
  35. #include <alloc.h>
  36. #else
  37. #ifdef __CORELEFT__
  38. #include <alloc.h>
  39. #endif
  40. #endif
  41.  
  42. /*--------------------------------------------------------------------*/
  43. /*                    UUPC/extended include files                     */
  44. /*--------------------------------------------------------------------*/
  45.  
  46. #include "lib.h"
  47. #include "dater.h"
  48. #include "logger.h"
  49.  
  50. /*--------------------------------------------------------------------*/
  51. /*                          Global variables                          */
  52. /*--------------------------------------------------------------------*/
  53.  
  54. #ifdef __HEAPCHECK__
  55. currentfile();
  56. #endif
  57.  
  58. int debuglevel = 1;
  59. FILE *logfile = stdout;
  60.  
  61. #ifdef __CORE__
  62. long  *lowcore = NULL;
  63. char  *copyright = (char *) 4;
  64. char  *copywrong = NULL;
  65. #endif
  66.  
  67. /*--------------------------------------------------------------------*/
  68. /*    As this routine is called from everywhere, we turn on stack     */
  69. /*    checking here to handle the off-chance we screwed up and        */
  70. /*    blew the stack.  This may catch it late, but it will catch      */
  71. /*    it.                                                             */
  72. /*--------------------------------------------------------------------*/
  73.  
  74. #ifdef __TURBOC__
  75. #pragma -N
  76. #else
  77. #pragma check_stack( on )
  78. #endif
  79.  
  80. char *full_log_file_name = "UUPC log file";
  81.  
  82. /*--------------------------------------------------------------------*/
  83. /*   p r i n t m s g                                                  */
  84. /*                                                                    */
  85. /*   Print an error message if its severity level is high enough.     */
  86. /*   Print message on standard output if not in remote mode           */
  87. /*   (call-in).  Always log the error message into the log file.      */
  88. /*                                                                    */
  89. /*   Modified by ahd 10/01/89 to check for Turbo C NULL pointers      */
  90. /*   being de-referenced anywhere in program.  Fixed 12/14/89         */
  91. /*                                                                    */
  92. /*   Modified by ahd 04/18/91 to use true variable parameter list,    */
  93. /*   supplied by Harald Boegeholz                                     */
  94. /*--------------------------------------------------------------------*/
  95.  
  96. void printmsg(int level, char *fmt, ...)
  97. {
  98.    va_list arg_ptr;
  99.  
  100. #ifdef __CORELEFT__
  101.    static unsigned freecore = 63 * 1024;
  102.    unsigned nowfree;
  103. #endif
  104.  
  105. #ifdef __HEAPCHECK__
  106.       static boolean recurse = FALSE;
  107.       int heapstatus;
  108.  
  109.       heapstatus = heapcheck();
  110.       if (heapstatus == _HEAPCORRUPT)
  111.          printf("\a*** HEAP IS CORRUPTED ***\a\n");
  112.  
  113. #endif
  114.  
  115. #ifdef __CORE__
  116.    if (*lowcore != 0L)
  117.    {
  118.       putchar('\a');
  119.       debuglevel = level;  /* Force this last message to print ahd   */
  120.    }
  121. #endif
  122.  
  123.  
  124. #ifdef __CORELEFT__
  125.    nowfree = coreleft();
  126.    if (nowfree < freecore)
  127.    {
  128.       freecore = (nowfree / 10) * 9;
  129.       printmsg(0,"Free memory = %u bytes", nowfree);
  130.    }
  131. #endif
  132.  
  133.    if (level <= debuglevel)
  134.    {
  135.  
  136.       FILE *stream = (logfile == NULL) ? stdout : logfile;
  137.  
  138.       va_start(arg_ptr,fmt);
  139.  
  140.       if (stream != stdout)
  141.       {
  142.          char now[DATEBUF];
  143.          vfprintf(stderr, fmt, arg_ptr);
  144.          fputc('\n',stderr);
  145.  
  146.          if ( debuglevel > 1 )
  147.             fprintf(stream, "(%d) ", level);
  148.          else
  149.             fprintf(stream, "%s ", dater( time( NULL ), now));
  150.  
  151.       } /* if (stream != stdout) */
  152.  
  153.       if (!ferror(stream))
  154.          vfprintf(stream, fmt, arg_ptr);
  155.  
  156.       if (!ferror(stream))
  157.          fputc('\n',stream);
  158.  
  159.       if (ferror(stream))
  160.       {
  161.          perror(full_log_file_name);
  162.          abort();
  163.       } /* if */
  164.  
  165. #ifdef __HEAPCHECK__
  166.       if ( !recurse )
  167.       {
  168.          recurse = TRUE;
  169. #ifdef __CORE__
  170.          if (*lowcore != 0L)
  171.             panic();
  172.     /*     if (!equal(copyright,copywrong))
  173.             panic();                         */
  174. #endif
  175.          if (heapstatus == _HEAPCORRUPT)
  176.             panic();
  177.          recurse = FALSE;
  178.       }
  179. #endif
  180.  
  181.  
  182. /*--------------------------------------------------------------------*/
  183. /*                        Massive debug mode?                         */
  184. /*--------------------------------------------------------------------*/
  185.  
  186.    if ((debuglevel > 10) &&  ((level+2) < debuglevel))
  187.       fflush( logfile );
  188.  
  189.    } /* if (level <= debuglevel) */
  190.  
  191. } /*printmsg*/
  192.